package com.example.asundae.particletest.boomview;

import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

/**
 * Created by super圣代 on 2017/6/11.
 */

/**
 * 在1.5秒内，通过ExplosionAnimator的方法getAnimatedValue()就能够不断得到递增的范围在0.0f~1.0f之间的值。
 */
public class ExplosionAnimator extends ValueAnimator {

    public static final int DEFAULT_DURATION = 1500;

    private Particle[][] particles;
    private ParticleFactory particleFactory;
    private Paint paint;
    private View container;

    public ExplosionAnimator(View view , Bitmap bitmap , Rect bound , ParticleFactory particleFactory) {
        setFloatValues(0.0f , 1.0f);
        setDuration(DEFAULT_DURATION);

        this.particleFactory = particleFactory;
        this.paint = new Paint();
        this.container = view;
        particles = particleFactory.generateParticles(bitmap , bound);

    }

    public void draw(Canvas canvas)
    {
        if(!isStarted())        ///动画是否结束 ， 防止多次点击
            return;

        for(Particle[] particle1 : particles)
        {
            for(Particle particle : particle1)
            {
                particle.advance(canvas , paint , (Float) getAnimatedValue());
            }
        }
        container.invalidate();
    }

    @Override
    public void start() {
        super.start();
        container.invalidate();
    }
}
